﻿namespace _03.InvalidRangeExceptionExersice
{
    using System;
    using System.Globalization;

    class InvalidRangeExceptionExersice
    {
        static void Main()
        {
            InvalidRangeException<int> invalidIntRangeException = new InvalidRangeException<int>("The value is out of the range.", 1, 100);
            InvalidRangeException<DateTime> invalidDateTimeRangeException =
                new InvalidRangeException<DateTime>("The value is out of range.", new DateTime(1980, 1, 1), new DateTime(2013, 12, 31));

            Console.WriteLine("Enter number in range [1-100]: ");
            try
            {
                int number = int.Parse(Console.ReadLine());
                if (number < invalidIntRangeException.Start || number > invalidIntRangeException.End)
                {
                    throw invalidIntRangeException;
                }
                else
                {
                    Console.WriteLine("The number {0} is in the range [0-100]", number);
                }

                Console.WriteLine("Enter Date [dd.MM.yyyy]: ");
                string format = "dd.MM.yyyy";
                DateTime dateTime;
                if (DateTime.TryParseExact(Console.ReadLine(), format, CultureInfo.InvariantCulture,
                    DateTimeStyles.None, out dateTime) && (dateTime < new DateTime(1980, 1, 1) || (dateTime > new DateTime(2013, 12, 31))))
                {
                    throw invalidDateTimeRangeException;
                }
                else
                {
                    Console.WriteLine("The date is in the range [01.01.1980-31.12.2013]");
                } 
            }
            catch (InvalidRangeException<int> ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (InvalidRangeException<DateTime> ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
